﻿using HtmlAgilityPack;
using JV.Com.Business;
using JV.Com.Internals;
using JV.Com.Model;
using JV.Com.Tools;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Documents;

namespace JV.Com.ViewModels
{
    public sealed class TestGamePageViewModel : PageBaseViewModel
    {
        private Test m_test;
        public TestGamePageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            m_test = new Test();
        }

        public event EventHandler TestLoadedEvent;
        public async override void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);

            InLoading = true;
            await LoadTestAsync((int)navigationParameter);
        }

        private async Task LoadTestAsync(int id)
        {
            m_test = await Service.GetGameTestAsync(id);

            var document = new HtmlDocument();
            document.LoadHtml(m_test.Content);

            var testContent = document.DocumentNode.ChildNodes.First(c => c.Attributes["id"] != null && c.Attributes["id"].Value == "test_txt");
            var converter = new Tools.HtmlToRichTextConverter(Tools.HtmlToRichTextConverter.ConvertType.Test);
            var inlinesContent = converter.GetHTMLInlineCollection(testContent.InnerHtml);
            m_contentParagraph = inlinesContent.ToParagraph();

            var recapContent = document.DocumentNode.ChildNodes.FirstOrDefault(c => c.Attributes["id"] != null && c.Attributes["id"].Value == "test_avis");
            if (recapContent != null)
            {
                var recapConverter = new Tools.HtmlToRichTextConverter(Tools.HtmlToRichTextConverter.ConvertType.TestRecap);
                var inlinesRecapContent = recapConverter.GetHTMLInlineCollection(recapContent.InnerHtml);
                m_recapParagraph = inlinesRecapContent.ToParagraph();
            }

            OnPropertyChanged("Rating");
            OnPropertyChanged("ImageTopUrl");
            OnPropertyChanged("PageTitle");
            OnPropertyChanged("GlobalReview");

            if (TestLoadedEvent != null) TestLoadedEvent(this, EventArgs.Empty);
        }

        private Paragraph m_contentParagraph;
        public Paragraph ContentParagraph { get { return m_contentParagraph; } }

        private Paragraph m_recapParagraph;
        public Paragraph RecapParagraph { get { return m_recapParagraph; } }

        public string ImageTopUrl { get { return m_test.ImageTopUrl; } }
        public int Rating { get { return m_test.Rating; } }
        public string GlobalReview { get { return m_test.GlobalReview; } }

        public override string PageTitle { get { return m_test.Title; } }

        #region IFlipViewFullScreenNavigator
        //private IEnumerable<string> m_flipViewImagesUrl;
        //IEnumerable<string> IFlipViewFullScreenNavigator.Images
        //{
        //    get { return m_flipViewImagesUrl; }
        //}

        //void IFlipViewFullScreenNavigator.NavigateToFlipView(IEnumerable<string> urls, int indexToLoad)
        //{
        //    var data = SerializerHelper.Serialize<FlipViewNavigationParameter>(new FlipViewNavigationParameter() { Images = urls, IndexToLoad = indexToLoad });
        //    NavigationService.Navigate("FlipViewFullScreen", data);
        //}

        public void NavigateToFlipView(string imageUrl)
        {
            //var data = SerializerHelper.Serialize<FlipViewNavigationParameter>(new FlipViewNavigationParameter() { Images = urls, IndexToLoad = indexToLoad });
            NavigationService.Navigate("FlipViewFullScreen", imageUrl);
        }
        #endregion
    }
}
